/*******************************************
Module: zt_pmalloc.h
E-Mail: 24zhutian@gmail.com
Notices: Copyright (c) 2007-2011 ls

memory pool: reference nginx(http://www.nginx.org) memory pool
*******************************************/
#ifndef zt_pmalloc_h
#define zt_pmalloc_h
#ifdef __cplusplus
extern "C" {
#endif
 
#define ZT_MAX_MALLOC_FROM_POOL 8*1024
#define ZT_DEFAULT_POOL_SIZE    16*1024
#define ZT_MIN_POOL_SIZE		4*1024
#define ZT_POOL_ALIGNMENT       16

typedef struct zt_mem_pool_s zt_mem_pool_t;

typedef struct zt_pool_large_s{
    size_t siz;
    u_char *str;
   
    struct zt_pool_large_s *next;
}zt_pool_large_t;

typedef struct zt_pool_data_s{
    size_t free;
    size_t use;

    zt_uint_t failed;

    u_char *str;
    u_char *last;

    zt_mem_pool_t *next;
}zt_pool_data_t;

struct zt_mem_pool_s{
    size_t max;			/* pool max alloc size */
	size_t size;		/* pool size size=max+sizeof(zt_mem_pool_t) */
    size_t count;		/* pool count */

    size_t siz;			/* large count */
    zt_int64_t msiz;	/* large mem size count */

    zt_pool_data_t pool;
    zt_mem_pool_t *current;
    zt_pool_large_t *large;
};

ZT_API zt_mem_pool_t *zt_mempool_create(size_t siz);
ZT_API void zt_mempool_destroy(zt_mem_pool_t *pool);
ZT_API void zt_mempool_reset(zt_mem_pool_t *pool);

ZT_API void *zt_pmalloc(zt_mem_pool_t *pool,size_t siz);
ZT_API void *zt_pnmalloc(zt_mem_pool_t *pool,size_t siz);
ZT_API zt_int_t zt_pfree(zt_mem_pool_t *pool,void *p);

#ifdef __cplusplus
}
#endif /* C++ */
#endif /* zt_pmalloc_h */
